package com.wsx.gradeanaly;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.WritableComparable;

public class Student implements WritableComparable<Student>, Cloneable {
	
	private String name;
	private float score;
	private char rank;
	
	public Student() {
	}
	
	public Student(String name, float score, char rank) {
		this.name = name;
		this.score = score;
		this.rank = rank;
	}
	
	public void setName(String name) {
		this.name = name;
	}

	public void setScore(float score) {
		this.score = score;
	}

	public void setRank(char rank) {
		this.rank = rank;
	}

	public String getName() {
		return name;
	}

	public float getScore() {
		return score;
	}

	public char getRank() {
		return rank;
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		this.name =  in.readUTF();
		this.score = in.readFloat();
		this.rank = in.readChar();
	}

	@Override
	public void write(DataOutput out) throws IOException {
		out.writeUTF(name);
		out.writeFloat(score);
		out.writeChar(rank);
	}
	
	@Override
	public String toString() {
		//return "Student [name=" + name + ", score=" + score + ", rank=" + rank + "]";
		return this.name+"\t"+this.score+"\t"+this.rank;
	}

	@Override
	public int compareTo(Student o) {
		String name = o.getName();
		float score = o.getScore();
		char rank = o.getRank();
		if(this.rank > rank){
			return 1;
		}else if(this.rank < rank){
			return -1;
		}
		if(this.score > score){
			return 1;
		}else if(this.score < score){
			return -1;
		}
		if(this.name.compareTo(name) > 0){
			return 1;
		}else if(this.name.compareTo(name) < 0){
			return -1;
		}
		return 0;
	}
	
	public Object clone()   
    {   
        Object o=null;
        try {
			o=(Student)super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
        return o;   
    }  
}
